package com.zx.kitchen.index.common.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.EndlessListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshEndlessListView;
import com.zx.kitchen.index.R;
import com.zx.kitchen.index.common.adapter.BusinessReportDetailAdapter;
import com.zx.kitchen.index.common.entity.BusinessReportDetailBean;
import com.zx.kitchen.index.common.entity.OrderBean;
import com.zx.kitchen.index.common.entity.ReportDayDetail;
import com.zx.kitchen.index.common.mvp.presenter.BusinessReportDetailPresenter;
import com.zx.kitchen.index.common.mvp.presenter.PurchaseReportDetailPresenter;
import com.zx.kitchen.index.common.mvp.view.BusinessReportDetailView;
import com.zx.kitchen.index.common.utils.TimeUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/16.
 */

public class BusinessReportDetailActivity extends Activity implements BusinessReportDetailView, View.OnClickListener {

    private ImageView toolbarLeft;
    private TextView toolbarTitle;
    private PullToRefreshEndlessListView mListview;
    private Button btnCount;
    private BusinessReportDetailPresenter businessReportDetailPresenter;
    private String day = "";
    private boolean type = false;
    private BusinessReportDetailAdapter businessReportDetailAdapter;
    private List<OrderBean> mList = new ArrayList<>();
    private PurchaseReportDetailPresenter purchaseReportDetailPresenter;
    private int p = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_business_report_detail);
        day = getIntent().getStringExtra("day");
        type = getIntent().getBooleanExtra("type", false);
        initView();
        initListener();

    }


    private void initView() {
        View view = findViewById(R.id.titleBar);
        toolbarLeft = (ImageView) view.findViewById(R.id.toolbar_leftLin);
        toolbarTitle = (TextView) view.findViewById(R.id.toolbar_title);
        toolbarTitle.setTextColor(getResources().getColor(R.color.blue));
        toolbarTitle.setText("财务报表详情");
        mListview = (PullToRefreshEndlessListView) findViewById(R.id.list_business_report_detail);
        btnCount = (Button) findViewById(R.id.btn_count);

        businessReportDetailPresenter = new BusinessReportDetailPresenter(this, getBaseContext());
        purchaseReportDetailPresenter = new PurchaseReportDetailPresenter(this, getBaseContext());
        Log.d("BusinessReportDetailAct", "采购or营业" + type);
        if (type) {
            purchaseReportDetailPresenter.getDataFromNet(getBaseContext(), "1", day);
        } else {
            businessReportDetailPresenter.getDataFromNet(getBaseContext(), "1", day);
        }

        businessReportDetailAdapter = new BusinessReportDetailAdapter(getBaseContext(), mList);
        mListview.setAdapter(businessReportDetailAdapter);

        mListview.getRefreshableView().setLoadingListener(new EndlessListView.PullLoadingListViewListener() {

            @Override
            public void onLoading() {
                mLoadMore();
            }
        });
        mListview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<EndlessListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<EndlessListView> refreshView) {
                String label = DateUtils.formatDateTime(getApplicationContext(),
                        System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
                refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
                mListview.getRefreshableView().resetAllLoadingComplete();
                mRefreshData();
            }
        });

    }

    private void mRefreshData() {
        onLoadData(true, type);
    }

    private void mLoadMore() {
        onLoadData(false, type);
    }

    private void onLoadData(boolean isRefresh, boolean isPurchase) {
        if (isRefresh) {
            p = 1;
        } else {
            p++;
        }
        if (type) {
            purchaseReportDetailPresenter.getDataFromNet(getBaseContext(), "1", day);
        } else {
            businessReportDetailPresenter.getDataFromNet(getBaseContext(), "1", day);
        }
    }


    private void initListener() {
        toolbarLeft.setOnClickListener(this);
        btnCount.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        onLoadData(false, type);
    }

    @Override
    public void onSuccess(ReportDayDetail reportDayDetail) {
        mListview.onRefreshComplete();
        mListview.getRefreshableView().loadingCompleted();
        mList.clear();
        mList.addAll(reportDayDetail.getDetail());
        businessReportDetailAdapter.notifyDataSetChanged();

    }

    @Override
    public void onFail(String str) {
        mListview.onRefreshComplete();
        mListview.getRefreshableView().loadingCompleted();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_leftLin:
                finish();
                break;
            case R.id.btn_count:
                startActivity(new Intent(this, BusinessReportActivity.class));
                break;
        }
    }
}
